#include<bits/stdc++.h>
#define rg register
#define file(x)freopen(x".in","r",stdin);freopen(x".out","w",stdout)

using namespace std;

const int maxn=5e2+10;

int n,H[maxn];
int F[maxn][maxn][maxn];

int main()
{
	file("build");
	scanf("%d",&n);
	for(rg int i=1;i<=n;i+=1)scanf("%d",H+i);
	for(rg int i=1;i<=n;i+=1)
		for(rg int x=0;x<n;x+=1)
			F[i][i][x]=H[i]!=(x+1);
	for(rg int len=2;len<=n;len+=1)
		for(rg int i=1;i+len-1<=n;i+=1)
		{
			rg int j=i+len-1;
			for(rg int x=0;x<=i-1+n-j;x+=1)
			{
				F[i][j][x]=(int)1e9;
				int Min=x+1,Max=n-((i-1+n-j)-x);
				F[i][j][x]=min(F[i][j][x],F[i+1][j][x+1]+(H[i]!=Min));
				F[i][j][x]=min(F[i][j][x],F[i][j-1][x+1]+(H[j]!=Min));
				F[i][j][x]=min(F[i][j][x],F[i+1][j][x]+(H[i]!=Max));
				F[i][j][x]=min(F[i][j][x],F[i][j-1][x]+(H[j]!=Max));
			}
		}
	printf("%d\n",F[1][n][0]);
	return 0;
}
